/*
* @File Name helloworld.c
* @File Path M:\MAS2\PRM_Robotic_Arm\PRMCcode\coding\acc\helloworld.c
* @Author: Ruige_Lee
* @Date:   2019-07-20 20:28:57
* @Last Modified by:   Ruige_Lee
* @Last Modified time: 2019-07-21 14:01:36
* @Email: 295054118@whut.edu.cn
* @page: https://whutddk.github.io/
*/
/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>

#include "xil_printf.h"



#include "xaxicdma.h"
#include "xdebug.h"
#include "xil_exception.h"
#include "xil_cache.h"
#include "xparameters.h"


#include "xscugic.h"
//#include "xpseudo_asm_gcc.h"
//#include "xreg_cortexa9.h"

/******************** Constant Definitions **********************************/


#define DMA_CTRL_DEVICE_ID	XPAR_AXICDMA_0_DEVICE_ID
#define INTC_DEVICE_ID		XPAR_SCUGIC_SINGLE_DEVICE_ID
#define DMA_CTRL_IRPT_INTR	XPAR_FABRIC_AXICDMA_0_VEC_ID
#define BUFFER_BYTESIZE		8192	/* Length of the buffers for DMA transfer */
#define RESET_LOOP_COUNT	10	/* Number of times to check reset is
					 * done
					 */


#define NUMBER_OF_TRANSFERS	4	/* Number of simple transfers to do */

static int DoSimplePollTransfer(XAxiCdma *InstancePtr, int Length, int Retries);
static int CheckData(u8 *SrcPtr, u8 *DestPtr, int Length);
int XAxiCdma_SimplePollExample(u16 DeviceId);

volatile uint8_t SrcBuffer[BUFFER_BYTESIZE] = {0,1,2,3,4,5,6,7,8,9,10};
__attribute__ ((section("dataBf2"))) volatile uint8_t DestBuffer[BUFFER_BYTESIZE] = {0,0,0,0,0};

//volatile uint8_t SrcBuffer[BUFFER_BYTESIZE] = {0,1,2,3,4,5,6,7,8,9,10};
//volatile uint8_t DestBuffer[BUFFER_BYTESIZE] = {0,0,0,0,0};

///* Source and Destination buffer for DMA transfer.*/
//volatile static u8 SrcBuffer[BUFFER_BYTESIZE] __attribute__ ((aligned (64)));
//volatile static u8 DestBuffer[BUFFER_BYTESIZE] __attribute__ ((aligned (64)));

extern void xil_printf(const char *format, ...);
/************************** Variable Definitions *****************************/

static XAxiCdma AxiCdmaInstance;	/* Instance of the XAxiCdma */


/* Shared variables used to test the callbacks.*/
volatile static int Done = 0;	/* Dma transfer is done */
volatile static int Error = 0;	/* Dma Bus Error occurs */


int main()
{
    print("Hello World\n\r");

    int Status;

    	xil_printf("\r\n--- Entering main() --- \r\n");

    	/* Run the poll example for simple transfer */
    	Status = XAxiCdma_SimplePollExample(DMA_CTRL_DEVICE_ID);

    	if (Status != XST_SUCCESS) {
    		xil_printf("AxiCdma_SimplePoll Example Failed\r\n");
    		return XST_FAILURE;
    	}

    	xil_printf("Successfully ran AxiCdma_SimplePoll Example\r\n");
    	xil_printf("--- Exiting main() --- \r\n");

    	return XST_SUCCESS;


}


int XAxiCdma_SimplePollExample(u16 DeviceId)
{
	XAxiCdma_Config *CfgPtr;
	int Status;
	int SubmitTries = 10;	/* try 10 times on submission */
	int Tries = NUMBER_OF_TRANSFERS;
	int Index;


	CfgPtr = XAxiCdma_LookupConfig(DeviceId);
	if (!CfgPtr) {
		return XST_FAILURE;
	}

	Status = XAxiCdma_CfgInitialize(&AxiCdmaInstance, CfgPtr,
		CfgPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}


	XAxiCdma_IntrDisable(&AxiCdmaInstance, XAXICDMA_XR_IRQ_ALL_MASK);

	for (Index = 0; Index < Tries; Index++) {
		Status = DoSimplePollTransfer(&AxiCdmaInstance,
			BUFFER_BYTESIZE, SubmitTries);
		if (Status != XST_SUCCESS) {
				return XST_FAILURE;
		}
	}


	return XST_SUCCESS;
}

static int DoSimplePollTransfer(XAxiCdma *InstancePtr, int Length, int Retries)
{
	int Index;
	u8  *SrcPtr;
	u8  *DestPtr;
	int Status;


	SrcPtr = (u8 *)SrcBuffer;
	DestPtr = (u8 *)DestBuffer;
	for (Index = 0; Index < BUFFER_BYTESIZE; Index++) {
		SrcPtr[Index] = Index & 0xFF;
		DestPtr[Index] = 0;
	}


	Xil_DCacheFlushRange((UINTPTR)&SrcBuffer, Length);

	while (Retries) {
		Retries -= 1;

		Status = XAxiCdma_SimpleTransfer(InstancePtr, (UINTPTR)SrcBuffer,
			(UINTPTR)DestBuffer, Length, NULL, NULL);
		if (Status == XST_SUCCESS) {
			break;
		}
	}


	if (!Retries) {
		return XST_FAILURE;
	}


	while (XAxiCdma_IsBusy(InstancePtr)) {
		/* Wait */
	}

	Error = XAxiCdma_GetError(InstancePtr);
	if (Error != 0x0) {
		int TimeOut = RESET_LOOP_COUNT;

		/* Need to reset the hardware to restore to the correct state
		 */
		XAxiCdma_Reset(InstancePtr);

		while (TimeOut) {
			if (XAxiCdma_ResetIsDone(InstancePtr)) {
				break;
			}
			TimeOut -= 1;
		}

		/* Reset has failed, print a message to notify the user
		 */
		return XST_FAILURE;
	}

	/* Transfer completes successfully, check data
	 */
	Status = CheckData(SrcPtr, DestPtr, Length);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	return XST_SUCCESS;
}

static int CheckData(u8 *SrcPtr, u8 *DestPtr, int Length)
{
	int Index;

	Xil_DCacheInvalidateRange((UINTPTR)DestPtr, Length);


	for (Index = 0; Index < Length; Index++) {
		if ( DestPtr[Index] != SrcPtr[Index]) {
			return XST_FAILURE;
		}
	}

	return XST_SUCCESS;
}



